package Middleware

import (
	"context"
	"fmt"
	"github.com/minio/minio-go/v7"
	"github.com/minio/minio-go/v7/pkg/credentials"
	"gopkg.in/ini.v1"
	"log"
	"os"
)

var MinioClient *minio.Client

func InitMinio() {
	// 初始化 MinIO 客户端
	var err error
	//从配置文件中获取配置

	cfg, err := ini.Load("./Config/config.ini")
	if err != nil {
		log.Fatalf("Failed to load config: %v", err)
	}
	//判断是笔记本还是台式
	hostname, _ := os.Hostname()
	var MinioAddress string
	var MinioSecretKey string
	var id string
	if hostname == "cai-pc" {
		MinioAddress = cfg.Section("minio_pc").Key("host").String() + ":" + cfg.Section("minio_pc").Key("port").String()
		id = cfg.Section("minio_pc").Key("access_key").String()
		MinioSecretKey = cfg.Section("minio_pc").Key("secret_key").String()
	} else if hostname == "cai-laptop" {
		MinioAddress = cfg.Section("minio_laptop").Key("host").String() + ":" + cfg.Section("minio_laptop").Key("port").String()
		id = cfg.Section("minio_laptop").Key("access_key").String()
		MinioSecretKey = cfg.Section("minio_laptop").Key("secret_key").String()
	} else {
		log.Fatalf("Error initializing MinIO client: %v", err)
	}
	fmt.Println("MinioAddress:", MinioAddress)
	fmt.Println("MinioSecretKey:", MinioSecretKey)
	fmt.Println("id:", id)

	//MinioClient, err = minio.New("192.168.10.130:9000", &minio.Options{ //台式机
	MinioClient, err = minio.New(MinioAddress, &minio.Options{ //笔记本
		//Creds: credentials.NewStaticV4("Admin_Image_ASKey", "m3sq4J7KCKXyGaAFXPGXNbqdmdABcoViobsJyuo5", ""), //192.168.3.242
		Creds:  credentials.NewStaticV4(id, MinioSecretKey, ""), //笔记本localhost
		Secure: false,
	})
	if err != nil {
		log.Fatalf("Error initializing MinIO client: %v", err)
	}

	// 创建名为 admin-file 的桶，如果不存在
	exists, err := MinioClient.BucketExists(context.Background(), "admin-file")

	if err != nil {
		log.Printf("Error checking bucket existence: %v", err)
		// 根据错误类型进行不同的处理
		if minio.ToErrorResponse(err).Code == "InvalidBucketName" {
			log.Printf("Invalid bucket name. Please check the bucket name format.")
		} else if minio.ToErrorResponse(err).Code == "NoSuchBucket" {
			log.Printf("Bucket does not exist. Creating it now.")
			err = MinioClient.MakeBucket(context.Background(), "admin-file", minio.MakeBucketOptions{})
			if err != nil {
				log.Fatalf("Error creating bucket: %v", err)
			}
		} else {
			log.Fatalf("Unknown error while checking bucket existence: %v", err)
		}
	} else if !exists {
		err = MinioClient.MakeBucket(context.Background(), "admin-file", minio.MakeBucketOptions{})
		if err != nil {
			log.Fatalf("Error creating bucket: %v", err)
		}
	}

	// 检查名为“indeximg”的桶是否存在并创建如果不存在
	indexImgExists, indexImgErr := MinioClient.BucketExists(context.Background(), "indeximg")
	if indexImgErr != nil {
		log.Printf("Error checking indeximg bucket existence: %v", indexImgErr)
		if minio.ToErrorResponse(indexImgErr).Code == "InvalidBucketName" {
			log.Printf("Invalid bucket name for indeximg. Please check the bucket name format.")
		} else if minio.ToErrorResponse(indexImgErr).Code == "NoSuchBucket" {
			log.Printf("indeximg bucket does not exist. Creating it now.")
			indexImgErr = MinioClient.MakeBucket(context.Background(), "indeximg", minio.MakeBucketOptions{})
			if indexImgErr != nil {
				log.Fatalf("Error creating indeximg bucket: %v", indexImgErr)
			}
		} else {
			log.Fatalf("Unknown error while checking indeximg bucket existence: %v", indexImgErr)
		}
	} else if !indexImgExists {
		indexImgErr = MinioClient.MakeBucket(context.Background(), "indeximg", minio.MakeBucketOptions{})
		if indexImgErr != nil {
			log.Fatalf("Error creating indeximg bucket: %v", indexImgErr)
		}
	}
}
